package com.streambolics.data;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.Inheritance;
import javax.jdo.annotations.InheritanceStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.streambolics.std.AnchorBuilder;

/**
 * The base class of all persistent objects that have a simple identity.
 * 
 * @author Sauron
 * 
 */

@PersistenceCapable(identityType = IdentityType.APPLICATION)
@Inheritance(strategy = InheritanceStrategy.SUBCLASS_TABLE)
public class SimpleObject extends PersistentObject
{
    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id;

    /**
     * Adds an anchor to the given list, referencing the object.
     * 
     * @param aBuilder
     *            The builder where the anchor will be added.
     * @param aLink
     *            The URL to use. This URL will be appended with the internal ID
     *            of the current object.
     * @param aCaption
     *            The caption to be used for the link.
     */

    protected void generateIdLink (AnchorBuilder aBuilder, String aLink, String aCaption)
    {
        aBuilder.addAnchor (getIdUrl (aLink), aCaption);
    }

    /**
     * The internal ID of the object.
     * 
     * @return A positive integer uniquely representing the object.
     */

    public long getId ()
    {
        return id;
    }

    /**
     * The internal ID represented as a string.
     * 
     * @return A string with the decimal representation of the internal ID.
     */

    public String getIdAsString ()
    {
        return Long.toString (id);
    }

    /**
     * An URL appended with the internal ID of the object.
     * 
     * @param aLink
     *            The base link.
     * @return The base link appended with the internal ID of the object.
     */

    public String getIdUrl (String aLink)
    {
        return aLink + "?ID=" + getIdAsString ();
    }
}
